<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-head-widget" :style="getStyle('head')">
      <template v-if="true">
        <div class="item left-wrap"></div>
        <div class="item middle-wrap" :style="getStyle('picture')">
          <img class="logo" alt="企业Logo" :src="pictureResource" v-if="(!!(optionsInfo?.imageUrl?.val) || (optionsInfo?.imageUrl))"/>
        </div>
        <div class="item right-wrap" :class="timeClassName(optionsInfo.changePosition)">
          <div class="time-info">{{ time }}</div>
          <div class="data-info" v-if="(optionsInfo.dateShow)">
            <div class="week">{{ getWeekInfo(week) }}</div>
            <div class="date">{{ date }}</div>
          </div>
        </div>
      </template>
    </div>
  </CompOperate>
</template>

<script>
import dayjs from 'dayjs';
import {NoticeBar} from 'vant';
import emitter from "@/utils/designer/emitter";
import {getWeek, pxToVw} from '@/utils/common';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import {buildParentApi} from "@/utils/designer/util";

export default {
  name: "scene-head-center-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    NoticeBar
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  data() {
    return {
      week: '',
      optionsInfo: Object(),
      mockOpen: false,
      date: dayjs().format('YYYY年MM月DD日'),
      time: dayjs().format('HH:mm'),
    }
  },

  computed: {

    timeClassName() {
      return function (val) {
        return val ? 'date-time' : 'time-date'
      }
    },

    timeType() {
      return this.optionsInfo.timeValue
    },

    // 获取日期
    getWeekInfo() {
      return function (info) {
        return getWeek(info)
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case 'picture':
            Reflect.set(Obj, 'height', pxToVw(info.imageHeight));
            Reflect.set(Obj, 'width', pxToVw(info.imageWidth));
            Reflect.set(Obj, 'borderRadius', pxToVw(info.imageRadius));
            return Obj;
          case 'title':
            Reflect.set(Obj, 'fontSize', pxToVw(info.fontSize));
            return Obj;
          case 'head':
            Reflect.set(Obj, 'height', pxToVw(info.headHeight));
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            Reflect.set(Obj, 'color', `${(info.fontColor)}`);
            return Obj;
        }
      }
    },

    //获取LOGO资源
    pictureResource() {
      let { imageUrl } = this.optionsInfo;
      if (this.$lodash.isObject(imageUrl)) {
        return imageUrl?.val?.url || imageUrl?.url;
      } else {
        return imageUrl;
      }
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.updateTime();
    this.handleOnMounted();
  },

  methods: {
    // 获取时间
    updateTime(val) {
      let timeType = (this.optionsInfo.timeValue);
      if (!!val) {
        let msg = JSON.parse(val);
        if (!!msg && !!(msg.timestamp)) {
          let timestamp = dayjs.unix(msg.timestamp);
          [this.week, this.date, this.time] = dayjs(timestamp).format(`d,${timeType},HH:mm`).split(',');
        } else {
          [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
        }
      } else {
        [this.week, this.date, this.time] = dayjs().format(`d,${timeType},HH:mm`).split(',');
      }
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "headerInfo":
          this.updateTime(data)
          break
      }
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

  watch: {
    // 获取配置项
    field: {
      handler: function (val) {
        let key = (val?.options?.config);
        this.optionsInfo = val.options[key];
      },
      deep: true,
      immediate: true
    },

    // 监听时间更新
    timeType: {
      handler: function (val) {
        if (val) {
          this.updateTime()
        }
      },
      deep: true,
      immediate: true
    },
  }

};
</script>

<style lang="scss" scoped>
.scene-head-widget {
  padding: 28px 30px;
  box-sizing: border-box;
  background-color: transparent;
  display: flex;
  justify-content: space-between;
  color: #333;
  height: 150px;
  overflow: hidden;
  .item {
    flex: 1.5;
  }
  .middle-wrap {
    margin-top: 50px;
    display: flex;
    flex: 2.4;
    flex-shrink: 0;
    overflow: hidden;
    align-self: center;
    justify-content: center;

    img {
      height: 90px !important;
      max-width: 100% !important;
      object-fit: contain;
    }
  }

  .right-wrap {
    overflow: hidden;
    line-height: 1;
    display: flex;
    flex-wrap: nowrap;
    justify-content: flex-end;
    flex-shrink: 0;
    align-self: center;

    .time-info {
      float: right;
      margin-right: 8px;
      font-size: 90px;
      vertical-align: top;
      white-space: nowrap;
      box-sizing: border-box;
      align-self: center;
      padding-right: 20px;
    }

    .data-info {
      float: right;
      align-self: center;
      font-size: 32px;
      .week {
        text-align: right;
        white-space: nowrap;
      }

      .date {
        font-weight: 500;
        white-space: nowrap;
        box-sizing: border-box;
        margin-top: 10px;
      }
    }
  }

  //时间日期交换位置
  .date-time {
    .time-info {
      // 2024-08-29 zk 1.5后位置不能交换
      //order: 1.5;
      order: 1;
      padding-right: 0;
    }

    .data-info {
      margin-right: 8px;
      padding-right: 20px;
    }
  }

}
</style>
